package  test;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
public class Main1 {
    public static void main(String[] args) {
        // 历史开奖数据（假设已经获取）
        List<int[]> history = getNum();
        history.add(new int[]{2, 5, 7, 10, 18, 26, 16}); // 第一期
        history.add(new int[]{3, 8, 12, 16, 21, 26, 7}); // 第二期
        history.add(new int[]{5,07 ,9 ,17 ,23 ,26, 12}); // 第三期
        history.add(new int[]{06, 11 ,17 ,24 ,27 ,33,16}); // 第一期
        history.add(new int[]{01 ,02 ,9 ,10 ,19 ,28,14}); // 第二期
        history.add(new int[]{05 ,06 ,11 ,24 ,28 ,33,07}); // 第三期
        history.add(new int[]{02 ,03 ,11 ,26 ,27 ,28,16}); // 第一期
        history.add(new int[]{04 ,05 ,10 ,16 ,31 ,33,07}); // 第二期
        history.add(new int[]{03 ,14 ,15 ,21 ,26 ,27}); // 第三期
        // 统计每个位置上每个数字的出现次数
        Map<Integer, Map<Integer, Integer>> counts = new HashMap<>();
        for (int[] numbers : history) {
            for (int i = 0; i < 6; i++) {
                int number = numbers[i];
                if (!counts.containsKey(i)) {
                    counts.put(i, new HashMap<>());
                }
                Map<Integer, Integer> frequency = counts.get(i);
                if (!frequency.containsKey(number)) {
                    frequency.put(number, 0);
                }
                frequency.put(number, frequency.get(number) + 1);
            }
        }
        // 计算每个位置上最可能出现的数字
        Set<Integer> candidates = new HashSet<>();
        for (int i = 0; i < 6; i++) {
            Map<Integer, Integer> frequency = counts.get(i);
            int bestNumber = -1;
            int bestCount = -1;
            for (Map.Entry<Integer, Integer> entry : frequency.entrySet()) {
                int number = entry.getKey();
                int count = entry.getValue();
                if (count > bestCount || (count == bestCount && Math.random() > 0.5)) {
                    bestNumber = number;
                    bestCount = count;
                }
            }
            candidates.add(bestNumber);
        }
        // 输出预测结果
        System.out.print("红球推荐：");
        for (int number : candidates) {
            System.out.print(number + " ");
        }
        System.out.println();
        System.out.print("蓝球推荐：");
        System.out.println(getBlueBall(candidates));
    }
    private static int getBlueBall(Set<Integer> candidates) {
        int maxNumber = 16; // 蓝球最大数字
        List<Integer> numbers = new ArrayList<>(candidates);
        int sum = 0;
        for (int i = 0; i < numbers.size(); i++) {
            sum += numbers.get(i);
        }
        return sum % maxNumber + 1;
    }

    public static List<int[]> getNum(){
        List<int[]> history = new ArrayList<>();
        // 创建httpclient实例
        CloseableHttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling().build();                                     //
        // 创建HttpGet实例，设置请求参数
        HttpGet httpGet = new HttpGet("http://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=5000&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=5000&week=&systemType=PC&dayStart=2018-11-28&dayEnd=2023-04-07");
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).build();
        httpGet.setConfig(requestConfig);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36");
        httpGet.addHeader("content-type","application/json;charset=UTF-8");
        httpGet.addHeader("Accept","*/*");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Cookie","HMF_CI=1fd1a5272428f0ed7cf3234fd81be30bf69146b3f289fe9d1871945e9d2f71647c01a7739bb4a46954bd78cd5f874214280d30707e9d0f1bce714b2271d2c6e2fe; 21_vq=7");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("Connection","keep-alive");


        try {
            // 执行http get请求
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            // 获取请求结果
            HttpEntity httpEntity = httpResponse.getEntity();
            String jsonString = EntityUtils.toString(httpEntity);
            // 解析JSON数据
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            JSONArray resultArray = jsonObject.getJSONArray("result");
            System.out.println("resultArray::"+resultArray);

            for (int i = 0; i < resultArray.size(); i++) {
                int[] redBallCount = new int[7];
                JSONObject resultObject = resultArray.getJSONObject(i);

                String numStr =  resultObject.getString("red") + "," + resultObject.getString("blue");
                // 获取开奖号码
                String[] numStrArray = numStr.split(",");
                // 更新红球号码出现的次数
                int i1 = 0;
                for (String num : numStrArray) {
                    redBallCount[i1] = Integer.parseInt(num);
                    i1++;
                }
                history.add(redBallCount);
            }


            return  history;
            
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放连接
            httpGet.releaseConnection();
        }
        return  history;
    }
}